{{Extend "db/index"}}
{{Block "title"}}{{"数据"|T}}: {{Stored.table}} - {{Super}}{{/Block}}
{{Block "breadcrumb"}}
{{Super}}
<li><a href="{{dbMgrURL "listTable" Stored.dbName}}">{{"数据库"|T}}: {{Stored.dbName}}</a></li>
<li><a href="{{dbMgrURL "viewTable" Stored.dbName Stored.table}}">{{"表"|T}}: {{Stored.table}}</a></li>
<li class="active">{{"数据"|T}}</li>
{{/Block}}
{{Block "main"}}
<script>
function onselectOrder(obj){
    if(!$(obj).val())return;
    var iptGrp=$(obj).parent();
    if(iptGrp.next('.input-group').length<1){
        var clone=iptGrp.clone();
        clone.find('select > option:checked,input:checkbox:checked').prop('checked',false);
        iptGrp.after(clone);
    }
}
function onselectWhere(obj){
    if(!$(obj).val())return;
    var iptGrp=$(obj).parent();
    if(iptGrp.next('.input-group').length<1){
        var clone=iptGrp.clone();
        clone.find('input:text').val('');
        clone.find('select > option:checked').prop('checked',false);
        iptGrp.after(clone);
    }
}
function onselectColumns(obj){
    if(!$(obj).val())return;
    var iptGrp=$(obj).parent();
    if(!iptGrp.hasClass('input-group'))iptGrp=iptGrp.parent();
    if(iptGrp.next('.input-group').length<1){
        var clone=iptGrp.clone();
        clone.find('input:text').val('');
        clone.find('select > option:checked').prop('checked',false);
        iptGrp.after(clone);
    }
}
</script>
<div class="row">
    <div class="col-md-12">
        <div class="block-flat row no-margin-top">
            <form action="" method="get">
              <input type="hidden" name="driver" value="{{Form "driver"}}">
              <input type="hidden" name="username" value="{{Form "username"}}">
              <input type="hidden" name="operation" value="{{Form "operation"}}">
              <input type="hidden" name="table" value="{{Stored.table}}">
              <input type="hidden" name="db" value="{{Stored.dbName}}">
              <div class="col-md-3">
              <label class="control-label">{{"查询"|T}}</label>
                  {{$cols := FormValues "columns[col][]"}}
                  {{range $key,$val := FormValues "columns[fun][]"}}
              <div class="input-group">
                  <select name="columns[fun][]" class="form-control" onchange="onselectColumns(this)">
					  <option value=""></option>
                      <optgroup label="{{"函数"|T}}">
                          {{range $k,$func := Stored.functions}}
                          <option value="{{$func}}"{{if eq $val $func}} selected{{end}}>{{$func}}</option>
                          {{end}}
                      </optgroup>
                      <optgroup label="{{"集合"|T}}">
                          {{range $k,$group := Stored.grouping}}
                          <option value="{{$group}}"{{if eq $val $group}} selected{{end}}>{{$group}}</option>
                          {{end}}
                      </optgroup>
                  </select>
                  {{$col := IndexStrSlice $cols $key}}
                  <label class="input-group-addon no-padding-bottom no-padding-top">(
                  <select name="columns[col][]" onchange="onselectColumns(this)">
					  <option value=""></option>
                      {{range $k, $colName := Stored.fields}}
                      <option value="{{$colName}}"{{if eq $col $colName}} selected{{end}}>{{$colName}}</option>
                      {{end}}
                  </select>
                  )</label>
              </div>
                  {{end}}
              </div>
              <div class="col-md-4">
              <label class="control-label">{{"搜索"|T}}</label>
                  {{$cols := FormValues "where[col][]"}}
                  {{$operators := FormValues "where[op][]"}}
                  {{range $key,$val := FormValues "where[val][]"}}
                  {{$col := IndexStrSlice $cols $key}}
                  {{$ope := IndexStrSlice $operators $key}}
              <div class="input-group">
                  <select name="where[col][]" class="form-control" onchange="onselectWhere(this)">
                      <option value="">({{"任意位置"|T}})</option>
                      {{range $k, $colName := Stored.fields}}
					  <option value="{{$colName}}"{{if eq $col $colName}} selected{{end}}>{{$colName}}</option>
                      {{end}}
                  </select>
                  <span class="input-group-addon no-padding-bottom no-padding-top"><select name="where[op][]">
                      {{range $k, $op := Stored.operators}}
					  <option value="{{$op}}"{{if eq $ope $op}} selected{{end}}>{{$op}}</option>
                      {{end}}
                  </select></span>
                  <input type="text" name="where[val][]" value="{{$val}}" class="form-control" onchange="onselectWhere(this)">
              </div>
                  {{end}}
              </div>
              <div class="col-md-2">
              <label class="control-label">{{"排序"|T}}</label>
                  {{$descs := FormValues "desc[]"}}
                  {{range $key,$val := FormValues "order[]"}}
                  {{$desc := IndexStrSlice $descs $key}}
              <div class="input-group">
                  <select name="order[]" class="form-control" onchange="onselectOrder(this)">
					  <option value=""></option>
                      {{range $k, $colName := Stored.fields}}
					  <option value="{{$colName}}"{{if eq $val $colName}} selected{{end}}>{{$colName}}</option>
                      {{end}}
                  </select>
                  <label class="input-group-addon no-padding-bottom no-padding-top"><select name="desc[]">
					  <option value="0"{{if eq $desc "0"}} selected{{end}}>{{"升序"|T}}</option>
                      <option value="1"{{if eq $desc "1"}} selected{{end}}>{{"降序"|T}}</option>
                  </select></label>
              </div>
                  {{end}}
              </div>
              <div class="col-md-3">
              <label class="control-label">{{"其它设置"|T}}</label>
              <div class="input-group">
                  <label for="limit" class="input-group-addon">{{"范围"|T}}</label>
                  <input type="number" name="limit" id="limit" class="form-control" value="{{Form "limit"}}">
            </div>
              <div class="input-group">
                  <label for="text_length" class="input-group-addon">{{"文本显示长度"|T}}</label>
                  <input type="number" name="text_length" id="text_length" class="form-control" value="{{Form "text_length"}}">
              </div>
              </div>
              <button type="submit" class="btn btn-primary btn-flat col-md-12"><span class="fa fa-search"></span> {{"查询"|T}}</button>
            </form>
        </div><!-- /.block-flat -->
        {{Include "db/mysql/results"}}
        <div class="block-flat row">
          <div class="header">
			<a href="{{dbMgrURL "createData" Stored.dbName Stored.table}}" class="btn btn-success pull-right">
				<i class="fa fa-plus"></i>
				{{"新建数据"|T}}
			</a>
            <h3>{{"数据"|T}}</h3>
          </div>
          <div class="content">
             <form method="POST" action="">
             <div class="table-responsive">
				<table class="table no-border hover blue">
					<thead class="no-border">
                        <tr>
                            <th style="width:50px">
                                <input type="checkbox" name="checkedAll" class="icheck" value="1" id="checkedAll">
                            </th>
                            <th style="width:50px">
                                <a href="{{dbMgrURL "listData" Stored.dbName Stored.table}}&modify=1" title="{{"编辑"|T}}"><i class="fa fa-pencil"></i></a>
                            </th>
                            {{range $k, $colName := Stored.columns}}
							<th><strong>{{$colName}}</strong></th>
                            {{end}}
						</tr>
					</thead>
					<tbody class="no-border-x" id="tbody-content">
                        {{range $k,$v := Stored.values}}
						<tr>
                            <td>
                                <input type="checkbox" class="icheck" name="check[]" value="{{uniqueIdf $v}}">
                            </td>
                            <td>
                                <a href="{{dbMgrURL "createData" Stored.dbName Stored.table}}{{uniqueIdf $v}}" title="{{"编辑"|T}}"><i class="fa fa-pencil"></i></a>
                            </td>
                            {{range $k, $colName := Stored.columns}}
                            <td>{{(index $v $colName).String}}</td>
                            {{end}}
						</tr>
                        {{end}}
					</tbody>
				</table>
			    </div>
                <fieldset>
                     <legend>{{"已选中"|T}} (<span id="selected-items-count">0</span>) {{T "共有%d项" Stored.total}}</legend>
                     <!--<button type="submit" class="btn btn-default" name="save" value="edit">{{"编辑"|T}}</button>
                     <button type="submit" class="btn btn-default" name="save" value="copy">{{"复制"|T}}</button>-->
                     <button type="submit" class="btn btn-danger" name="save" value="delete">{{"删除"|T}}</button>
                </fieldset>
             </form>
             {{Stored.pagination.Render}}
          </div><!-- /.content -->
        </div><!-- /.block-flat -->
    </div>
</div>
{{/Block}}

{{Block "footer"}}
{{Super}}

{{Modal "__TMPL__/db/mysql/copy_table.yaml"}}
{{Modal "__TMPL__/db/mysql/move_table.yaml"}}
<script type="text/template" id="tpl-move-table-form">
    <div class="form-group">
        <label>{{"移动表"|T|ToJS}}</label>
        <textarea readonly="readonly" class="form-control" id="move-tables">
        </textarea>
    </div>
    <div class="form-group">
        <label>{{"到数据库"|T|ToJS}}</label>
        <select class="form-control" id="move-table-to-db">
			<option value="" disabled>loading...</option>
        </select>
    </div>
</script>
<script type="text/template" id="tpl-copy-table-form">
    <div class="form-group">
        <label>{{"复制表"|T|ToJS}}</label>
        <textarea readonly="readonly" class="form-control" id="copy-tables">
        </textarea>
    </div>
    <div class="form-group">
        <label>{{"到数据库"|T|ToJS}}</label>
        <select class="form-control" id="copy-table-to-db">
			<option value="" disabled>loading...</option>
        </select>
    </div>
</script>
<script>
var currentDb='{{Stored.dbName}}';
function refreshList(){
    $.get(window.location.href,{'_pjax':'#tbody-content'},function(r){
        var e=$(r);
        $('#tbody-content').html(e.find('#tbody-content').html());
        perttyCheckbox();
    },'html');
}
function getSelectedItems(){
    var tables=[],views=[];
    $('#tbody-content input[name="table[]"]:checked').each(function(){
        tables.push($(this).val());
    });
    $('#tbody-content input[name="view[]"]:checked').each(function(){
        views.push($(this).val());
    });
    return {tables:tables,views:views};
}
function perttyCheckbox(){
    $('#selected-items-count').text($('#tbody-content .icheck:checked').length);
    App.iCheck('#tbody-content .icheck','click',function(){
        var v=$(this).prop('checked')?1:-1;
        var c=$('#selected-items-count');
        var n=parseInt(c.text());
        c.text(n+v);
    });
}
$(function(){
    perttyCheckbox();

    $('#move-table-modal .modal-body').html($('#tpl-move-table-form').html());
    $('#copy-table-modal .modal-body').html($('#tpl-copy-table-form').html());
    $('#move-table-modal .btn-primary:last').removeClass('md-close').on('click',function(){
        var dbName=$('#move-table-to-db').val();
        var se=getSelectedItems();
        $.post(window.location.href,{json:'move',dbName:dbName,table:se.tables,view:se.views},function(r){
            if(r.Code==1){
                refreshList();
                $('#move-table-modal').niftyModal('hide');
                App.showDbLog(r.Data);
            }else{
                alert(r.Info);
            }
        },'json');
    });
    $('#copy-table-modal .btn-primary:last').removeClass('md-close').on('click',function(){
        var dbName=$('#copy-table-to-db').val();
        var se=getSelectedItems();
        $.post(window.location.href,{json:'copy',dbName:dbName,table:se.tables,view:se.views},function(r){
            if(r.Code==1){
                refreshList();
                $('#copy-table-modal').niftyModal('hide');
                App.showDbLog(r.Data);
            }else{
                alert(r.Info);
            }
        },'json');
    });

    $('#move-selected,#copy-selected').on('click',function(event){
		var op,tx;
		if($(this).attr('id')=='move-selected'){
			op='move';
			tx='{{"移动"|T}}';
		}else{
			op='copy';
			tx='{{"复制"|T}}';
		}
        var se=getSelectedItems();
        if(se.tables.length<1&&se.views.length<1){
            return App.message({title:'{{"出错了"|T}}',text:String('{{"请选择您要{op}的表"|T}}').replace(/\{op\}/,tx),class_name:'danger'},false);
        }
        var sed=se.tables.join(',');
        if(se.views.length>0){
            if(sed)sed+=',';
            sed+=se.views.join(',');
        }
		$('#'+op+'-tables').val(sed);
		$('#'+op+'-table-modal').niftyModal('show',{afterOpen:function(modal){
            if(!$('#'+op+'-table-to-db').data('loaded')){
                $('#'+op+'-table-to-db').data('loaded',1);
                $.get(window.location.href,{json:'dbs'},function(r){
                    if(r.Code<1){
                        return App.message(r.Info);
                    }
                    var str='';
                    for(var i=0;i<r.Data.length;i++){
						var s=r.Data[i]==currentDb?' selected':'';
                        str+='<option value="'+r.Data[i]+'"'+s+'>'+r.Data[i]+'</option>';
                    }
                    $('#'+op+'-table-to-db').html(str);
                },'json');
            }
        }});
	});
    $('#analyze-selected,#optimize-selected,#check-selected,#repair-selected,#truncate-selected,#drop-selected').on('click',function(event){
        var op,tx;
        switch($(this).attr('id')){
            case 'analyze-selected':
			op='analyze';
			tx='{{"分析"|T}}';
            break;
            case 'optimize-selected':
			op='optimize';
			tx='{{"优化"|T}}';
            break;
            case 'check-selected':
			op='check';
			tx='{{"检查"|T}}';
            break;
            case 'repair-selected':
			op='repair';
			tx='{{"修复"|T}}';
            break;
            case 'truncate-selected':
			op='truncate';
			tx='{{"清空"|T}}';
            break;
            case 'drop-selected':
			op='drop';
			tx='{{"删除"|T}}';
            break;
        }
        var se=getSelectedItems();
        if(se.tables.length<1&&se.views.length<1){
            return App.message({title:'{{"出错了"|T}}',text:String('{{"请选择您要{op}的表"|T}}').replace(/\{op\}/,tx),class_name:'danger'},false);
        }
        if(op=='truncate'||op=='drop'){
            var sed=se.tables.join('\n');
            if(se.views.length>0){
                if(sed)sed+='\n';
                sed+=se.views.join('\n');
            }
            if(!confirm(String('{{"确定要{op}下面这些表吗？"|T}}').replace(/\{op\}/,tx)+'\n'+sed)){
                return;
            }
        }
        $.post(window.location.href,{json:op,table:se.tables,view:se.views},function(r){
            var hasData=typeof(r.Data)!='undefined';
            if(r.Code==1||(hasData && r.Data && r.Data.length>1)){
                refreshList();
                if(hasData&&r.Data){
                    for(var i=0;i<r.Data.length;i++){
                        App.showDbLog(r.Data[i]);
                    }
                }
            }else{
                App.message({title:'{{"出错了"|T}}',text:r.Info,class_name:'danger'},false);
            }
        },'json');
    });
});
</script>
{{/Block}}